package indi.h.targetcall;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import indi.h.targetcall.calls.SThreadToolCall;
import indi.h.targetcall.manager.STManager;

public class Test implements Cloneable {
	public static synchronized void say() {
		System.out.println("sb");
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void sea() {
		System.out.println("zb");
	}

	public static void main(String[] args) throws Exception {
		ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(3);
		final SThreadToolCall<String, ExecutorService> st = new SThreadToolCall<>(new STManager<>());
		//st.reset("sb", newFixedThreadPool);

		// ExecutorService newFixedThreadPool=Executors.newFixedThreadPool(5);
		// for (int i = 0; i < 1; i++) {
		// final int a = i;
		// newFixedThreadPool.submit(new Runnable() {
		//
		// @Override
		// public void run() {
		// // TODO Auto-generated method stub
		// try {
		// System.out.println("xxx:" + a);
		// Thread.sleep(1000);
		// //System.out.println(a);
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// }
		// });
		// }

		// for (int i = 0; i < 20; i++) {
		// final int a = i;
		// new Thread() {
		// public void run() {
		// ExecutorService newFixedThreadPool1 =
		// Executors.newFixedThreadPool(3);
		// try {
		// st.reset("sb", newFixedThreadPool1, null);
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// };
		// }.start();
		// }
		// new Thread() {
		// public void run() {
		// ExecutorService newFixedThreadPool1 =
		// Executors.newFixedThreadPool(3);
		// try {
		// st.reset("tb", newFixedThreadPool1, null);
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// };
		// }.start();
		// new Thread() {
		// public void run() {
		// ExecutorService newFixedThreadPool1 =
		// Executors.newFixedThreadPool(3);
		// try {
		// st.reset("lb", newFixedThreadPool1, null);
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// };
		// }.start();
		// new Thread() {
		// public void run() {
		// ExecutorService newFixedThreadPool1 =
		// Executors.newFixedThreadPool(3);
		// try {
		// st.reset("ub", newFixedThreadPool1, null);
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// };
		// }.start();

		// Test.say();
		// new Thread(){
		// public void run() {
		// System.out.println("2");
		// Test.sea();
		// };
		// }.start();
		// new Thread(){
		// public void run() {
		// System.out.println("1");
		// Test.say();
		// };
		// }.start();
		//
		//
		//
		// Thread.sleep(40000);

		// newFixedThreadPool.shutdown();
		// while (true) {
		// if (newFixedThreadPool.awaitTermination(1, TimeUnit.SECONDS)) {
		// break;
		// }else{
		// System.out.println("::::::::::::::::::::::::::::::::::::");
		// }
		// }
		// System.out.println("xxxxuuuuuuu");

		ExecutorCompletionService<Integer> ecs = new ExecutorCompletionService<Integer>(newFixedThreadPool);
		for (int i = 0; i < 10; i++) {
			final int a = i;

			ecs.submit(new Callable<Integer>() {

				@Override
				public Integer call() throws Exception {
					// TODO Auto-generated method stub
					System.out.println("xxx:" + a);
					//Thread.sleep(1000 * a);
					if(a==8){
						Thread.sleep(1000);
					}
					return a;
				}

			});
			//System.out.println(submit.get());
		}
		for (int i = 0; i < 10; i++) {
			System.out.println("--------------------------");
			Future take = ecs.take();
			System.out.println(take.get());
			System.out.println("++++++++++++++++++++++++++");
		}
		System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
		// newFixedThreadPool.shutdownNow();
		// System.out.println("ooooooooooooo");

	}

}
